/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.tms.service;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.ap.entity.Sl;
import com.jeeplus.modules.ap.service.SlService;
import com.jeeplus.modules.fee.entity.FeeZzsl;
import com.jeeplus.modules.fee.service.FeeZzslService;
import com.jeeplus.modules.infor.entity.Achievements;
import com.jeeplus.modules.infor.entity.NewCustomers;
import com.jeeplus.modules.infor.entity.PriceCustomers;
import com.jeeplus.modules.infor.entity.Start;
import com.jeeplus.modules.infor.mapper.NewCustomersMapper;
import com.jeeplus.modules.infor.mapper.SettlementMapper;
import com.jeeplus.modules.infor.mapper.StartMapper;
import com.jeeplus.modules.infor.service.NewCustomersService;
import com.jeeplus.modules.infor.service.SettlementService;
import com.jeeplus.modules.tms.entity.AdditionalCharges;
import com.jeeplus.modules.tms.entity.NewBill;
import com.jeeplus.modules.tms.entity.NewPlanning;
import com.jeeplus.modules.tms.mapper.AdditionalChargesMapper;
import com.jeeplus.modules.tms.mapper.NewBillMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.tms.entity.Dispatching;
import com.jeeplus.modules.tms.mapper.DispatchingMapper;

/**
 * 派车单Service
 * @author 李超辉
 * @version 2019-04-29
 */
@Service
@Transactional(readOnly = true)
public class DispatchingService extends CrudService<DispatchingMapper, Dispatching> {

	@Autowired
	private AdditionalChargesMapper additionalChargesMapper;
	@Autowired
	private NewBillService newBillMapper;
	@Autowired
	private NewCustomersService customerss;
	@Autowired
	private StartMapper startMapper;
	@Autowired
	private SettlementMapper settlementMapper;
	@Autowired
	private SlService slService;
	@Autowired
	private FeeZzslService zzslService;
	@Autowired
	private NewPlanningService newPlanningService;
	@Autowired
	private NewCustomersService newCustomersService;

	public Dispatching get(String id) {
		Dispatching dispatching = super.get(id);
		dispatching.setAdditionalChargesList(additionalChargesMapper.findList(new AdditionalCharges(dispatching)));
		return dispatching;
	}


	public List<Dispatching> findList(Dispatching dispatching) {
		return super.findList(dispatching);
	}
	
	public Page<Dispatching> findPage(Page<Dispatching> page, Dispatching dispatching) {
		return super.findPage(page, dispatching);
	}
	@Transactional(readOnly = false)
	public Page<Dispatching> findPage1(Page<Dispatching> page, Dispatching dispatching) {
		Page<Dispatching> list1=super.findPage(page,dispatching);
		List<Dispatching> list=list1.getList();
		for(Dispatching d:list){
			String[] ids=d.getRemarks().split(",");
			String zy="";
			String th="";
			double jsje=0.0;
			double jsjehs=0.0;
			double sj1=0.0;
			double mlr1=0.0;
			int js=0;

			NumberFormat nf = NumberFormat.getInstance();
			DecimalFormat df = new DecimalFormat("0.00%");
			for(String id:ids){
				NewBill n=newBillMapper.get(id);
				if(n.getQsjs()!=null&&n.getQsjs()!=""){
					js+=Integer.parseInt(n.getQsjs());
				}else{
					js+=0;
				}
				if(!zy.contains(n.getZydh())){
					String zydh=n.getZydh();
					zy+=zydh+",";
				}
				if(!th.contains(n.getThdh())){
					String thdh=n.getThdh();
					th+=thdh+",";
				}
				String dj=n.getPrice();
				if(dj==null||dj.equals("")){
					NewPlanning nb=newPlanningService.get(n.getPlanning());
					NewCustomers nc = newCustomersService.get(nb.getCustomer());
					List<PriceCustomers> li = nc.getPriceCustomersList();
					boolean bo = false;
					String  pc = null;
					for (PriceCustomers pp : li) {
						if (pp.getWidth().getWidth().equals(nb.getWidth()) && pp.getZylx().equals(nb.getZylx())) {
							pc = pp.getJsjg();
							bo = true;
						}
					}
					if(!bo){
						break;
					}else{
						n.setPrice(pc);
					}
				}
				dj=n.getPrice();
				Sl sl=slService.findUniqueByProperty("pro",n.getPro());
				sl=slService.get(sl.getId());
				String ssl=sl.getSl();
				FeeZzsl zzsl=zzslService.findUniqueByProperty("pro",n.getPro());
				zzsl=zzslService.get(zzsl.getId());
				String zzs=zzsl.getSl();
				String zl=n.getWeight();
				double js1=Double.parseDouble(zl)*Double.parseDouble(dj);
				double jshs=js1*(1+Double.parseDouble(ssl));
				String zzc=n.getZyf();
				double a=jshs*Double.parseDouble(zzs);
				String zc=n.getZyf();
				Double xs=0.0;
				double xx=0.0;
				double kp=0.0;
				if(n.getFktype().equals("线上")){
					xs+=Double.parseDouble(n.getZyf());
				}else if(n.getFktype().equals("开票")){
					kp+=Double.parseDouble(n.getZyf());
				}else{
					xx+=Double.parseDouble(n.getZyk());

				}
				double b=xs*73/1000;
				double c=xx*0.13;
				double d1=kp*0.09;
				double sj=a-b-c-d1;
				double mlr=jshs-sj-Double.parseDouble(zzc);
				double lrl=mlr/jshs;
				n.setJsje(nf.format(js1).replaceAll(",",""));
				n.setJsjehs(nf.format(jshs).replaceAll(",",""));
				n.setSj(nf.format(sj).replaceAll(",",""));
				n.setMlr(nf.format(mlr).replaceAll(",",""));
				n.setLrl(df.format(lrl));
				n.setSl(ssl);
				n.setZzsl(zzs);
				jsje+=Double.parseDouble(n.getJsje());
				jsjehs+=Double.parseDouble(n.getJsjehs());
				sj1+=Double.parseDouble(n.getSj());
				mlr1+=Double.parseDouble(n.getMlr());


				newBillMapper.save(n);
			}
			d.setThdh(th);
			d.setZydh(zy);
			String i=ids[0];
			NewBill bill=newBillMapper.get(i);
			d.setXdrq(bill.getDate());

			d.setQsjs(js+"");
			d.setFhrq(bill.getLkthd());
			d.setDgs(bill.getDgssj());
			d.setDhz(bill.getDhzsj());
			d.setDdlx(bill.getType());
			d.setZylx(bill.getZylx());
			d.setJsje(nf.format(jsje).replaceAll(",",""));
			d.setJsjehs(nf.format(jsjehs).replaceAll(",",""));
			d.setSj(nf.format(sj1).replaceAll(",",""));
			d.setMlr(nf.format(mlr1).replaceAll(",",""));
			d.setSl(bill.getSl());
			d.setZzsl(bill.getZzsl());
			double lrl=mlr1/jsjehs;
			d.setLrl(df.format(lrl));


			d.setQyd(startMapper.get(bill.getQyd()).getName());
			d.setDzs(bill.getDzs());
			d.setJsd(settlementMapper.get(bill.getJsd()).getName());
			d.setHtlc(bill.getHtlc());
			d.setJxyj("");
			NewCustomers b=customerss.get(bill.getCustomer());
			List<Achievements> l=b.getAchievementsList();
			for(Achievements a:l){
				if(a.getWidth().getWidth().equals(d.getJxdwqj())){
					d.setJxyj(a.getJxyj());
				}
			}

			double ysdj=Double.parseDouble(d.getZyf())/Double.parseDouble(d.getZzl());
			d.setYsdj(nf.format(ysdj).replaceAll(",",""));
			if(!d.getJxyj().equals("")){
				double wc=Double.parseDouble(d.getYsdj())/Double.parseDouble(d.getJxyj());
				d.setJxwcz(df.format(wc));
			}

			double ykvl=Double.parseDouble(d.getZyk())/Double.parseDouble(d.getZyf());
			d.setYkbl(df.format(ykvl));
			d.setShdwmc(bill.getShdw());
			d.setShdwdz(bill.getSite());
			d.setQssj(bill.getQssj());
			d.setQszt(bill.getQszt());
			d.setQsrdh(bill.getQsr());

		}
		page.setList(list);
		return page;

	}

	@Transactional(readOnly = false)
	public void save(Dispatching dispatching) {
		super.save(dispatching);
		for (AdditionalCharges additionalCharges : dispatching.getAdditionalChargesList()){
			if (additionalCharges.getId() == null){
				continue;
			}
			if (AdditionalCharges.DEL_FLAG_NORMAL.equals(additionalCharges.getDelFlag())){
				if (StringUtils.isBlank(additionalCharges.getId())){
					additionalCharges.setDis(dispatching);
					additionalCharges.preInsert();
					additionalChargesMapper.insert(additionalCharges);
				}else{
					additionalCharges.preUpdate();
					additionalChargesMapper.update(additionalCharges);
				}
			}else{
				additionalChargesMapper.delete(additionalCharges);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(Dispatching dispatching) {
		super.delete(dispatching);
		additionalChargesMapper.delete(new AdditionalCharges(dispatching));
	}


}